package net.zhengxinyun.performance.controller.gradePoint;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.GradePointEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.GradePointFilter;
import net.zhengxinyun.performance.service.gradePoint.GradePointService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/gradePoint")
public class GradePointController {

    private GradePointService gradePointService;
    private LoginService loginService;
    private TokenService tokenService;

    @Autowired
    public GradePointController(GradePointService gradePointService, LoginService loginService, TokenService tokenService) {
        this.gradePointService = gradePointService;
        this.loginService = loginService;
        this.tokenService = tokenService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody GradePointParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = gradePointService.insert(param.gradePointEntity);
            if (!"添加成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody GradePointParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = gradePointService.update(param.gradePointEntity);
            if (!"修改成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody GradePointParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = gradePointService.delete(param.gradePointEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }


    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody GradePointParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            HashMap<String, Object> map = new HashMap<>();
            List<GradePointFilter> list = gradePointService.query(param.gradePointFilter);
            if (list.size() == 0 && param.gradePointFilter.getPageNumber() != 0) {
                param.gradePointFilter.setPageNumber(param.gradePointFilter.getPageNumber() - 1);
            }
            list = gradePointService.query(param.gradePointFilter);
            Integer pageTotal = gradePointService.queryForCount(param.gradePointFilter);
            map.put("list", list);
            map.put("pageTotal", pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }
}

class GradePointParam {
    public GradePointFilter gradePointFilter;
    public GradePointEntity gradePointEntity;
    public TokenEntity tokenEntity;
}